package com.app.babygo;



import com.app.babygo.dbservice.AchievementAdapter;
import com.app.babygo.exceptions.InternalDBException;
import com.app.babygo.exceptions.ObjectNotFoundException;
import com.app.babygo.help.MessageHelper;
import com.app.babygo.obj.AchievementTask;
import com.app.babygo.obj.ChildAchievement;

import android.os.Bundle;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

public class TaskDetail extends Activity {
	
	protected static final String TASK_ID_KEY = "TaskID";
	protected static final String TAG = "TaskDetailView";
	protected static boolean needRefresh = false;
	protected TextView TaskStatusTV;
	protected ImageView AchievementImage;
	protected Button UnlockBtn;
	protected Context ViewContext;
	protected AchievementTask Task;
	protected ChildAchievement Achievement;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_task_detail);
		loadDataFromIntent();
		createUI();
	}
	
	protected void loadDataFromIntent() {
		Intent intent = this.getIntent();
		ViewContext = this.getApplicationContext();
		Task = (AchievementTask)intent.getSerializableExtra(TaskList.SELECTED_TASK_KEY);
		boolean isCompleted = intent.getBooleanExtra(TaskList.IS_COMPLETED_KEY, false);
		if (isCompleted) {
			Achievement = (ChildAchievement)intent.getSerializableExtra(TaskList.TASK_COMPLETE_DETAIL_KEY);
		} else {
			Achievement = null;
		}
	}
	
	protected void loadDataFromDB() {
		String taskID = Task.getTaskid();
		AchievementAdapter db = new AchievementAdapter(this);
		db.open();
		try {
			Task = db.getAchievementTask(taskID);
			Achievement = db.getChildAchievement(ChildChoose.SelectedChildID, taskID);
		} catch (ObjectNotFoundException e) {
			Log.e(TAG, "Task not found. Task ID:" + taskID);
			MessageHelper.showMsg(this, "Error", "Task is not exist");
			Task = null;
			Achievement = null;
		} catch (InternalDBException e) {
			Log.e(TAG, e.getMessage());
			MessageHelper.showMsg(this, "Internal DB Error", "Unable to get task.");
			Task = null;
			Achievement = null;
		} 
		db.close();
	}

	protected void createUI() {
		if (Task == null){
			Log.e(TAG, "Empty task");
			return;
		}
		
		TextView task_name = (TextView) this.findViewById(R.id.task_detail_title);
		AchievementImage = (ImageView) this.findViewById(R.id.achievement_pic);
		UnlockBtn = (Button)this.findViewById(R.id.unlock_btn);
		task_name.setText(Task.getTaskname());
		TextView task_description = (TextView) this.findViewById(R.id.task_detail_desription);
		task_description.setText(Task.getTaskdescription());
		TaskStatusTV = (TextView) this.findViewById(R.id.task_detail_status);
		if (Achievement != null) {
			String status = String.format("Completed on %tD", Achievement.getAchievementDate());
			TaskStatusTV.setText(status);
			Bitmap bitmap = BitmapFactory.decodeFile(Achievement.getPicturePath());
			AchievementImage.setImageBitmap(bitmap);
			AchievementImage.setVisibility(View.VISIBLE);
			UnlockBtn.setVisibility(View.GONE);
		} else {
			TaskStatusTV.setText("Not complete");
			AchievementImage.setVisibility(View.GONE);
			UnlockBtn.setVisibility(View.VISIBLE);
			UnlockBtn.setOnClickListener(new View.OnClickListener(){
				@Override
				public void onClick(View v) {
					Intent intent = new Intent(v.getContext(), TaskUnlock.class);
					intent.putExtra(TASK_ID_KEY, Task.getTaskid());
					startActivity(intent);
				}			
			});
		}
	}
	
	public static void expireCurrentView(){
		needRefresh = true;
	}
	
	public void refreshTaskDetail() {
		if (needRefresh) {
			loadDataFromDB();
			createUI();
			TaskList.expireCurrentView();
			needRefresh = false;
		}
	}
	
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.child_info, menu);
		return true;
	}
	
	@Override
	protected void onResume(){
		super.onResume();
		refreshTaskDetail();
	}
	
	@Override
	protected void onPause() {
		super.onPause();
		//finish();
		//overridePendingTransition(R.anim.slide_left_in, R.anim.slide_right_out); 
	}

}
